import AudioManager from "../Manager/EffectManager";

const { ccclass, property } = cc._decorator;

/**
 * UI主控制类
 * 控制UI交互逻辑
 */
@ccclass
export default class UIMain extends cc.Component {
  // propertys
  @property(cc.Node)
  homeViewNode: cc.Node = null

  @property(cc.Node)
  overViewNode: cc.Node = null

  @property(cc.Node)
  scoreLabel: cc.Node = null

  @property(cc.AudioSource)
  effectAudioSource: cc.AudioSource = null

  // 给外部设定的事件回调函数
  public startClickEventCallback: Function = null
  public restartClickEventCallback: Function = null
  public toHomeClickEventCallback: Function = null

  /**
   * 显示主页面
   */
  public showHomeView() {
    this.homeViewNode.active = true
  }

  public offHomeView() {
    this.homeViewNode.active = false
  }

  /**
   * 显示结束页面
   */
  public showOverView(score?: number) {
    this.overViewNode.active = true
    if (score != null) {
      this.scoreLabel.getComponent(cc.Label).string = '得分：' + score
    }
    // 关闭背景音乐
    // this.bgmAudioSource.stop()
  }

  public offOverView() {
    this.overViewNode.active = false
  }

  /**
   * 开始按钮点击事件
   */
  protected startBtnClickEvent(): void {
    if (this.startClickEventCallback != null) {
      this.startClickEventCallback()
    }
    // 播放按钮音效
    // this.effectPlay('button')
    // 播放背景音乐
    // this.bgmAudioSource.play()
    // 关闭主页面ui
    this.offHomeView()
  }

  /**
   * 重新开始点击事件
   */
  protected restartBtnClickEvent(): void {
    if (this.restartClickEventCallback != null) this.restartClickEventCallback()
    // 播放按钮音效
    // this.effectPlay('button')
    // 播放背景音乐
    // this.bgmAudioSource.play()
    // 关闭结束页面ui
    this.offOverView()
  }

  /**
   * 返回主页点击事件
   */
  protected toHomeBtnClickEvent(): void {
    if (this.toHomeClickEventCallback != null) this.toHomeClickEventCallback()
    // 播放按钮音效
    // this.effectPlay('button')
    // 关闭结束页面ui
    this.offOverView()
    // 显示主页面ui
    this.showHomeView()
  }

  // private effectPlay(effectName: string) {
  //   const audioManager = this.effectAudioSource.getComponent(AudioManager)
  //   audioManager.play(effectName)
  // }
}
